<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查看用户优惠券</title>
    <script src="/jquery/jquery-3.2.1.min.js"></script>
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/ztree/css/metroStyle/metroStyle.css"/>
    <script type="text/javascript" src="/ztree/js/jquery.ztree.all.min.js"></script>
    <script src="/Vue/vue-2.6.12.js"></script>
    <!--<link rel="stylesheet" href="/css/coupon/list/coupon-list.css"/>-->
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 100%;
            height: 1150px;
        }
        .list{
            width: 1650px;
            height: 50px;
            margin-left: 20px;
            background-color:rgb(243,243,243);
        }
        .list-left{
            width: 100px;
            height: 30px;
            margin-top: 10px;
            float: left;
        }
        .list-left-left{
            width: 10px;
            height: 30px;
            float: left;
            background: green;
        }
        .list-left-right{
            width: 80px;
            height: 30px;
            font-size: 14px;
            line-height: 30px;
            float: left;
            text-indent: 5px;
        }
        .list-right{
            float: right;
            margin-top: 5px;
            margin-right: 45px;
        }
        .query{
            width: 1650px;
            height: 100px;
            margin-top: 20px;
        }
        .layui-col-md11{
            width: 100%;
        }
        .layui-card-header{
            margin-left: 20px;
            width: 1620px;
            background-color:rgb(243,243,243);
        }
        .query-top-right{
            float: right;
            margin-right: 30px;
        }
        .layui-form-label{
            margin-left: 170px;
            width: 100px;
        }
        .layui-form-item .layui-input-inline {
            float: left;
            width: 250px;
            margin-right: 80px;
        }
        .record{
            width: 1650px;
            height: 700px;
            margin-top: 40px;
            margin-left: 20px;
            background-color:rgb(243,243,243);
        }

        .record-right{
            width: 1650px;
            height: 700px;
            float: left;
        }
        /*设置数据表格的单元格样式*/
        .layui-table-cell {
            line-height: 50px;
            height: inherit;
        }
        .layui-table-cell>a{
            color:green;
        }

    </style>
</head>
<body>
<div id="app" class="box">
    <!--商品列表-->
    <div class="list">
        <div class="list-left">
            <div class="list-left-left"></div>
            <p class="list-left-right">优惠券查询</p>
        </div>
        <div class="list-right">
            <div class="list-right-left layui-btn layui-icon layui-icon-refresh" id="Refresh">&nbsp;刷新</div>
            <div class="list-right-right layui-btn layui-icon layui-icon-left" id="goBack">&nbsp;返回</div>
        </div>
    </div>

    <!--筛选查询-->
    <div class="layui-row query">
        <div class="layui-col-md11">
            <div class="layui-card headerCenter">
                <div class="layui-card-header backColor">
                    <i class="layui-icon">&#xe615;</i>
                    筛选查询
                </div>
                <div class="layui-card-body">
                    <form class="layui-form" lay-filter="layForm">
                        <div class="layui-form-item">
                            <label class="layui-form-label">用户账号：</label>
                            <div class="layui-input-inline">
                                <input placeholder="请输入用户账号" name="account" class="layui-input" id="account"/>
                            </div>
                            <label class="layui-form-label">用户名称：</label>
                            <div class="layui-input-inline">
                                <input class="layui-input" name="nickname" placeholder="请输入需要查询的用户名称" id="nickname" type="text"/>
                                <input class="layui-input" name="userId" placeholder="" id="userId" type="hidden"/>
                            </div>
                            <span style="float: right;margin-right: 10px">
                                <button type="button" id="search" class="layui-btn">
                                    <i class="layui-icon layui-icon-search" ></i> 搜索
                                </button>
                                <button type="reset"  class="layui-btn layui-btn-warm">
                                    <i class="layui-icon layui-icon-refresh-3"></i> 重置
                                </button>
                            </span>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!--数据列表-->
    <div class="record">

        <div class="record-right">
            <table lay-filter="couponTable" id="couponTable"></table>
        </div>
    </div>
    <!--表头样式-->
    <div id="toolbarFunction" hidden class="layui-row" lay-filter="toolbarFunction">
        <div style="float: left;">
            <i class="layui-icon layui-icon-tabs"></i>数据列表
        </div>
    </div>
    <div id="toolFunction" lay-filter="toolFunction" hidden>
            <span style="margin: 0px 15px; color: #009688;" lay-event="toolCouponGive">赠送优惠券</span>

    </div>
    <!-- 返回 -->
    <p id="title" style="display: none;">查看用户优惠券</p>
</div>
</body>
    <script>
        layui.use(["layer",'jquery','laydate','table','form'],function () {
            var
                layer = layui.layer,
                $ = layui.jquery,
                table = layui.table;
            var user = JSON.parse(localStorage.getItem('user'));
            selectUserUsed("","");
            function selectUserUsed(account,nickname){
                $.ajax({
                    url:"/JsUserCouponUsed/getAllJsBeforeUser",
                    data:{"account":account,"nickname":nickname},
                    type:'post',
                    dataType:"json",
                    success:function (data) {
                        tableRender(data.data)
                    }
                });
            };
            //返回
            $('#goBack').click(function () {
                var title = $('#title').text();
                var iframe = parent.$('.iframe');
                $.each(iframe,function (i,item) {
                    if(title == $(item).attr('_href')){
                        parent.$('#beforeHref').val($(item).attr('_href'));
                        parent.beforeHref();
                        return;
                    }
                });
            });
            //刷新
            $("#Refresh").click(function () {
                location.reload();
            });
            $("#search").click(function () {
                var nickname = $("#nickname").val();
                var account = $("#account").val();
                selectUserUsed(account,nickname);
            });


            function tableRender(data){
                console.log(data);
                table.render({
                    elem:"#couponTable",
                    data:data,
                    toolbar:"#toolbarFunction",
                    defaultToolbar:false,
                    cols:[[
                        {type:'checkbox'},
                        {field:"userId",title:"用户Id",align:"center"},
                        {field:"account",title:"用户账号",align:"center"},
                        {field:"nickname",title:"用户昵称",align:"center"},
                        {field:"",title:"已使用",align:"center",templet:function (data) {
                            $("#userId").val(data.userId);
                            var param={
                                'usedState':3,
                                'userId':data.userId
                            }
                            var data2;
                                $.ajax({
                                    url:"/JsUserCouponUsed/getUsedCoupon",
                                    data:param,
                                    type:'post',
                                    async: false,
                                    dataType:'json',
                                    success:function (data1) {
                                        console.log(data1);
                                        data2 = data1.data;
                                    }
                                });

                                return "<a>"+data2+"</a>";

                            }},
                        {field:"",title:"未使用",align:"center",templet:function (data) {
                                var param={
                                    'usedState':2,
                                    'userId':data.userId
                                }
                                var data2;
                                $.ajax({
                                    url:"/JsUserCouponUsed/getUsedCoupon",
                                    data:param,
                                    type:'post',
                                    async: false,
                                    dataType:'json',
                                    success:function (data1) {
                                        data2 = data1.data;
                                    }
                                });
                                return "<a>"+data2+"</a>";

                            }},
                        {field:"",title:"已过期",align:"center",templet:function (data) {
                                var param={
                                    'usedState':4,
                                    'userId':data.userId
                                }
                                var data2;
                                $.ajax({
                                    url:"/JsUserCouponUsed/getUsedCoupon",
                                    data:param,
                                    type:'post',
                                    async: false,
                                    dataType:'json',
                                    success:function (data1) {
                                        data2 = data1.data;
                                    }
                                });
                                return "<a>"+data2+"</a>";

                            }},
                        {field:"",title:"操作",align:"center",toolbar:"#toolFunction"}
                    ]],
                    page:true
                })
                table.on("tool(couponTable)",function (obj) {
                    localStorage.setItem("dataUserId",obj.data.userId);
                    if(obj.event == "toolCouponGive"){
                        layer.open({
                            type:2,
                            title:'发放优惠券',
                            area:["1000px","600px"],
                            offset:['150px','400px'],
                            btn:["确定","取消"],
                            content:"give-coupon.html",
                            yes:function(index,layero){
                                var obj = layero.find('iframe').contents();
                                var couponId = obj.find("#couponId").val();
                                var issueQuantity = obj.find("#issueQuantity").val();
                                var dataUserId = localStorage.getItem("dataUserId");
                                if (issueQuantity==0){
                                    layer.msg("该优惠券剩余量为零！！！");
                                } else{
                                    var param = {
                                        "userId":dataUserId,
                                        "couponId":couponId,
                                        "issueQuantity":issueQuantity-1,
                                        "getDate":getTime(),
                                        "usedState":2
                                    };
                                    $.ajax({
                                        url:'/JsUserCouponUsed/addJsUserCouponUsed',
                                        data:param,
                                        type:'post',
                                        dataType:'json',
                                        success:function (data) {
                                            layer.close(index);
                                            selectUserUsed("","");
                                            layer.msg("添加成功");
                                        }
                                    });
                                }


                            },
                            btn2:function(layero,index){
                            }

                        })
                    }

                });
            }
            //现在时间
            function getTime(){
                var date=new Date();
                var year=date.getFullYear();
                var month=date.getMonth()+1;
                var day=date.getDate();
                var hour=date.getHours();
                var minute=date.getMinutes();
                var second=date.getSeconds();
                var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
                return dateStr;
            }
            function zeroFilling(data){
                if(data<10){
                    data="0"+data;
                }
                return data;
            }
        })
    </script>
</html>